import java.util.ArrayList;
import java.util.Date;

/**
 * This class just shows the functionality of the different classes.
 */
public class CustomerManagement {
	private final ArrayList<Customer> customers = new ArrayList<Customer>();
	private final DataStorage dataStorage;

	public static void main(String[] args) {
		// at first we have to initialize everything
		CSVCustomerSerializer cs = new DefaultCSVCustomerSerializer(";");
		DataStorage ds = new CSVFile(cs, "CustomerManagement.csv");
		CustomerManagement cm = new CustomerManagement(ds);
		
		// now we are ready and able to load everything
		cm.loadCustomers();
		
		// lets have a look
		cm.printCustomers();
		System.out.println("----");
		
		// now lets add new customer 		
		cm.addCustomer(new Customer(cm.getHighestId() + 1, Gender.MALE,
				"Schmitz", "Tom", Status.ACTIV, new Date(), 500, 100));
		
		// lets have a look again		
		cm.printCustomers();
		System.out.println("----");
		
		// and in the end we save our customers
		if (cm.saveCustomers()) {
			System.out.println("Everything saved");
		}
	}

	private CustomerManagement(DataStorage dataStorage) {
		this.dataStorage = dataStorage;
	}

	/**
	 * This function is used to add a customer to the list. It checks several
	 * things before the object is inserted into the list.
	 * 
	 * @param newCustomer
	 *            The new customer, not null.
	 * @throws IllegalArgumentException
	 *             The function will raise this exception, if <li>the
	 *             newCustomer is null</li> <li>the object is already in the
	 *             list</li> <li>there is already an object with the same id in
	 *             the list</li>
	 */
	public void addCustomer(Customer newCustomer)
			throws IllegalArgumentException {
		if (newCustomer == null) {
			throw new IllegalArgumentException(
					"A null object cannot be added to the list.");
		}

		// At first we have to check if there is an customer object with the
		// same id or if it is already into the list. In this case we cannot add
		// the new object to our list and we have to throw an exception.
		int id = newCustomer.getId();
		for (Customer customer : customers) {
			if (customer.getId() == id) {
				throw new IllegalArgumentException(
						"There is already a object with the same id in the list");
			}
		}

		customers.add(newCustomer);
	}

	/**
	 * @return The function returns the highest id of all customer objects. If
	 *         there is no customer object, the function will return -1.
	 */
	public int getHighestId() {
		int highestId = -1;

		for (Customer customer : customers) {
			if (customer.getId() > highestId) {
				highestId = customer.getId();
			}
		}

		return highestId;
	}

	public void loadCustomers() {
		customers.clear();
		customers.addAll(dataStorage.loadCustomers());
	}
	
	public boolean saveCustomers() {
		return dataStorage.saveCustomers(customers);
	}
	
	/**
	 * This function is used for debugging.
	 */
	private void printCustomers() {
		for (Customer customer : customers) {
			System.out.println(customer);
		}
	}
}
